package huaweiod.second;

/**
 * 山脉的个数
 * https://renjie.blog.csdn.net/article/details/134352444
 */
public class C1_MountainCount {

    public static int validMountainCount(int[] arr) {
        if (arr == null || arr.length < 2) {
            return 0;
        }
        int n = arr.length;
        int res = 0;
        if (arr[0] > arr[1]) {
            res++;
        }
        if (arr[n - 1] > arr[n - 2]) {
            res++;
        }
        for (int i = 1; i < arr.length - 1; i++) {
            if (arr[i - 1] < arr[i] && arr[i] > arr[i + 1]) {
                res++;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] arr1 = {0, 1, 2, 3, 2, 4};
        System.out.println(validMountainCount(arr1));    // 2
        int[] arr2 = {3, 0, 3, 4, 1};
        System.out.println(validMountainCount(arr2));    // 2
        int[] arr3 = {1, 0};
        System.out.println(validMountainCount(arr3));
    }
}
